package com.framework.auth.ext.password;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.core.OAuth2Token;
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenGenerator;

public class UserNamePasswordProviderBuilder {
    public static AuthenticationProvider build(HttpSecurity http, UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {
        OAuth2AuthorizationService authorizationService = http.getSharedObject(OAuth2AuthorizationService.class);
        OAuth2TokenGenerator<? extends OAuth2Token> tokenGenerator = http.getSharedObject(OAuth2TokenGenerator.class);

        UserNamePasswordProvider provider =
                new UserNamePasswordProvider(authorizationService, tokenGenerator, userDetailsService, passwordEncoder);

        return provider;
    }
}
